<link rel="stylesheet" href="__STATIC_LIB__layuiadmin/layui/layui_ext/dtree/dtree.css">
<link rel="stylesheet" href="__STATIC_LIB__layuiadmin/layui/layui_ext/dtree/font/dtreefont.css">

<form class="layui-form  seller-alone-form" id="userAdd" style="padding:20px;">

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label"><i class="required-color">*</i>部/院：</label>
            <div class="layui-input-inline" id="dep_box">
                <select name="dep_id" lay-filter="dep_id" id="dep_id" list="dep_list" lay-search>
                    <option value=""></option>
                    <datalist  id="dep_list" lay-search>
                        {volist name="deptList" id="deptList"}
                        <option value="{$deptList.bmdm}">{$deptList.bmmc}</option>
                        {/volist}
                    </datalist>
                </select>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label"><i class="required-color">*</i>科教中心：</label>
            <div class="layui-input-inline">
                <input type="text" name="sub_center_name" lay-verify="sub_center_name" id="sub_center_name" class="layui-input">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label"><i class="required-color">*</i>专业：</label>
            <div class="layui-input-inline" id="specialty_box">
                <select name="specialty_id" lay-filter="specialty_id" id="specialty_id" lay-search>
                    <option value=""></option>
                </select>
            </div>
        </div>
    </div> 
    
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button id="subcenter_add_save" class="layui-btn" lay-submit="" lay-filter="save">保存</button>
        </div>
    </div>
    {:zlserverToken()}
</form>
<script src="__STATIC_LIB__layuiadmin/layui/layui.js"></script>
<script>
    layui.extend({
        dtree: '__STATIC_LIB__layuiadmin/layui/layui_ext/dtree/dtree'
    }).use(['form', 'laydate', 'dtree'], function () { 
        var form = layui.form, laydate = layui.laydate, dtree = layui.dtree;
        form.render();
 
        //保存用户
        form.on('submit(save)', function(data){
            var dep_id = data.field.dep_id;
            var sub_center_name = data.field.sub_center_name;
            var specialty_id = data.field.specialty_id;
            
            if(!dep_id){
                layer.msg('部/院不能为空！', {time: 1300});
                return false;
            }
            if(!sub_center_name){
                layer.msg('科教中心不能为空！', {time: 1300});
                return false;
            }
            if(!specialty_id){
                layer.msg('专业不能为空！', {time: 1300});
                return false;
            }

            $("#subcenter_add_save").addClass("layui-btn-disabled");
            $('#subcenter_add_save').attr("disabled", true);

            $.ajax({
                type: 'post',
                url: '{:url("BookDraw/subcenteradd")}',
                data: data.field,
                dataType: 'json',
                success: function (e) { 
                    if(e.status === true){ 
                        layer.open({
                            type: 1
                            ,offset: 'auto'
                            ,id: 'layerDemoAuto'
                            ,content: '<div style="padding: 20px 100px;">'+ e.msg +'</div>'
                            ,btn: '关闭'
                            ,btnAlign: 'c' //按钮居中
                            ,shade: 0.3 //显示遮罩
                            ,yes: function(){
                                parent.layui.admin.events.changeTabAndRefresh('/manage/book_draw/subcenter.html', 'subcenterTable');
                            },close: function() {
                                console.log('close');
                            },end: function () {
                                parent.layui.admin.events.changeTabAndRefresh('/manage/book_draw/subcenter.html', 'subcenterTable');
                            }
                        });
                    }else{
                        $("#subcenter_add_save").removeClass("layui-btn-disabled");
                        $('#subcenter_add_save').removeAttr("disabled");

                        layer.msg(e.msg, {time: 1300});
                    }
                }
            });
            return false;
        });
    
        layui.use('form', function () {
        // 联动部门
            form.on('select(dep_id)', function (data) {
                getSpecialty(data);
            });
        });
 
        // 得到专业
        function getSpecialty(data) {
            $.ajax({
                type: 'get',
                url: "{:url('BookCommon/getSpecicalties')}",
                data: 'bmdm=' + data.value,
                success: function(e) {
                    if(e.status) {
                        $("#specialty_id").empty();//清空下拉框的值
                        $('#specialty_id').append(new Option('', ''));// 下拉菜单里添加元素
                        
                        $("#class_id").empty();//清空下拉框的值
                        $('#class_id').append(new Option('', ''));// 下拉菜单里添加元素
                        $.each(e.data.data, function (index, item) {
                            $('#specialty_id').append(new Option(item.zymc, item.zydm));// 下拉菜单里添加元素
                        });
                        layui.form.render("select");//重新渲染 固定写法
                    } else {
                        layer.msg(e.msg);
                    }
                }
            });
        };

    });
</script>
